package com.paytos.cuoheonline.common;

import com.alibaba.fastjson.JSON;

import java.io.Serializable;

/**
 * 返回结果对象
 * @author: 彭伟煌(pengweihuang@xmutca.com)
 * @create: 2016-07-08 22:52
 */
public class Result<T> implements Serializable {

    public static class Status {

        /**
         * 业务处理成功
         */
        public final static int SUCCESS = 200;

        /**
         * 请求超时
         */
        public final static int TIMEOUT = 408;

        /**
         * 业务访问失败
         */
        public final static int ERROR = 500;

        /**
         * 页面重定向
         */
        public final static int REDIRECT = 302;

        /**
         * 权限代码
         */
        public final static int AUTH = 403;

        /**
         * 请进行登陆
         */
        public final static int NEED_LOGIN = 401;

        /**
         * 页面不存在
         */
        public final static int NOT_FOUND = 404;

        /**
         * 错误请求
         */
        public final static int BAD_REQUEST = 400;

        /**
         * 无法满足条件的，认定为攻击
         */
        public final static int NOT_ACCEPTABLE = 406;
    }

    private String message;

    private int status = Status.SUCCESS;

    private Long timestamp = System.currentTimeMillis();

    private T result;

    private Object extra;

    public Result() {

    }

    public Result(int status) {
        this.status = status;
    }

    public Result(String message) {
        this.message = message;
    }

    public Result(int status, T result) {
        this.status = status;
        this.result = result;
    }

    public Result(int status, String message, T result) {
        this.status = status;
        this.message = message;
        this.result = result;
    }

    public Result(int status, String message, Long timestamp, T result) {
        this.status = status;
        this.message = message;
        this.timestamp = timestamp;
        this.result = result;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public Object getExtra() {
        return extra;
    }

    public void setExtra(Object extra) {
        this.extra = extra;
    }

    /**
     * 快速builder工具
     * @param <T>
     */
    public static class Builder<T> {

        private String message;

        private int status = Status.SUCCESS;

        private long timestamp = System.currentTimeMillis();

        private T result;

        public Builder status(int status) {
            this.status = status;
            return this;
        }

        public Builder message(String message) {
            this.message = message;
            return this;
        }

        public Builder result(T result) {
            this.result = result;
            return this;
        }

        public Builder timestamp(long timestamp) {
            this.timestamp = timestamp;
            return this;
        }

        public Builder result(Result<T> resultObject) {
            this.status = resultObject.getStatus();
            this.message = resultObject.getMessage();
            this.timestamp = resultObject.getTimestamp();
            this.result = resultObject.getResult();
            return this;
        }

        public Result<T> build() {
            Result<T> resultObj = new Result<T>();
            resultObj.setMessage(message);
            resultObj.setResult(result);
            resultObj.setTimestamp(timestamp);
            resultObj.setStatus(status);
            return resultObj;
        }
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
